//
//  RegisterViewController.m
//  YSSmartHome
//
//  Created by Yahaong on 16/8/16.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "RegisterViewController.h"
#import "TextFieldLeftView.h"
#import "SettingInfoViewController.h"
#import "MQTTSessionManager.h"
#import "UIView+HUD.h"
#import "GlobalConst.h"
#import <XAISDK/XAIHTTPManager.h>

@interface RegisterViewController ()

@property (nonatomic, strong) UITextField *mTextPhone;
@property (nonatomic, strong) TextFieldLeftView * mViewPhoneLeft;
@property (nonatomic, strong) UITextField *mTextSms;
@property (nonatomic, strong) UIButton *mBtnSms;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) TextFieldLeftView * mViewPwd;
@property (nonatomic, strong) UITextField *mTextPwd;
@property (nonatomic, strong) UIButton *mBtnTreaty;
@property (nonatomic, strong) UIButton *m_btnNext;
@property (nullable, nonatomic, strong) UIView* mViewPhoneBackground;
@property (nullable, nonatomic, strong) UIView* mViewSmsBackground;
@property (nullable, nonatomic, strong) UIView* mViewSplit;
@property (nullable, nonatomic, strong) UIView* mViewPwdBackground;
@end

@implementation RegisterViewController

#pragma mark - life cycle

- (void)dealloc {
    debugLogInfo(@"RegisterViewController dealloc");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (!kMQTTSessionManager.xai) {
        kMQTTSessionManager.xai = [[XAI alloc]initWithHost:kZTMQTTBaseURL port:kZTMQTTBasePort];
    }
    [self initSubViews];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

#pragma mark - event Response

- (void)mBtnTreatyClick:(UIButton *)btn
{
    btn.selected = !btn.selected;
}

- (void)sure
{
    if (![MQTTCoverTool isMobileNumber:self.mTextPhone.text]) {
        [self.view yh_showMsgHUD:@"你输入的手机号不正确"];
        return;
    }
    if(self.mTextPwd.text.length < 6){
        [self.view yh_showMsgHUD:@"密码长度不对"];
        return;
    }
    if (!self.mBtnTreaty.selected && self.flag) {
        [self.view yh_showMsgHUD:@"你还没有阅读相关条约"];
        return;
    }
    if (self.mTextSms.text.length < 4 )
    {
        [self.view yh_showMsgHUD:@"验证码不正确"];
        return;
    }
    if (self.flag) //注册
    {
        [kMQTTSessionManager.xai registerWithAccount:self.mTextPhone.text password:self.mTextPwd.text barCode:self.mTextSms.text success:^{
            [self.view yh_showMsgHUD:@"成功"];
            [[NSNotificationCenter defaultCenter]postNotificationName:kZTSwitchRootViewControllerKey object:@(2)];
        } error:^(NSError * _Nonnull error) {
            [self.view yh_showMsgHUD:error.userInfo[@"errormsg"]];
        }];
    }
    else //重置
    {
        [kMQTTSessionManager.xai pwdResetChangeWithAccount:self.mTextPhone.text newPwd:self.mTextPwd.text barCode:self.mTextSms.text success:^{
            [self.view yh_showMsgHUD:@"成功"];
            [[NSNotificationCenter defaultCenter]postNotificationName:kZTSwitchRootViewControllerKey object:@(2)];
        } error:^(NSError * _Nonnull error) {
            [self.view yh_showMsgHUD:error.userInfo[@"errormsg"]];
        }];
    }
}

- (void)mBtnSmsClick
{
    if (self.timer)
    {
        return;
    }
    [self sendAuthMessage];
}

- (void)update
{
    static NSInteger count = 60;
    if (count <= 0)
    {
        [self.mBtnSms setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.timer invalidate];
        self.timer = nil;
        self.mBtnSms.userInteractionEnabled = YES;
        count = 60;
        return;
    }
    [self.mBtnSms setTitle:[NSString stringWithFormat:@"重新获取(%02zd)",count--] forState:UIControlStateNormal];
}


#pragma mark - private method
- (void)sendAuthMessage
{
    __weak typeof(self)wself = self;
    [kMQTTSessionManager.xai sendAuthMessage:self.mTextPhone.text success:^{
        __strong typeof(wself) sself = wself;
        if (!sself) { return; }
        sself.mBtnSms.userInteractionEnabled = NO;
        sself.timer = [NSTimer timerWithTimeInterval:1.0 target:sself selector:@selector(update) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop ] addTimer:sself.timer forMode:NSRunLoopCommonModes];
    } error:^(NSError * _Nonnull error) {
        __strong typeof(wself) sself = wself;
        if (!sself) { return; }
        [sself.view yh_showMsgHUD:error.userInfo[@"errormsg"]];
    }];
}

- (void)initSubViews {
    self.view.backgroundColor = kBgColor;
    [self.view addSubview:self.mViewPhoneBackground];
    [self.mViewPhoneBackground addSubview:self.mViewPhoneLeft];
    [self.mViewPhoneBackground addSubview:self.mTextPhone];
    [self.view addSubview:self.mViewSmsBackground];
    [self.mViewSmsBackground addSubview:self.mBtnSms];
    [self.mViewSmsBackground addSubview:self.mViewSplit];
    [self.mViewSmsBackground addSubview:self.self.mTextSms];
    [self.mViewSmsBackground addSubview:self.mBtnSms];
    [self.view addSubview:self.mViewPwdBackground];
    [self.mViewPwdBackground addSubview:self.mViewPwd];
    [self.mViewPwdBackground addSubview:self.mTextPwd];
    [self.view addSubview:self.mBtnTreaty];
    [self.view addSubview:self.m_btnNext];
    [self layoutPageSubViews];
}

- (void)layoutPageSubViews
{
    [self.mViewPhoneBackground mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(64 + kIphoneSize_Height(20));
        make.leading.trailing.equalTo(self.view);
        make.height.equalTo(kIphoneSize_Height(50));
    }];
    [self.mViewPhoneLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.equalTo(self.mViewPhoneBackground);
        make.width.equalTo(kIphoneSize_Widith(50));
    }];
    [self.mTextPhone mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(self.mViewPhoneLeft.trailing).offset(kIphoneSize_Widith(10));
        make.top.bottom.trailing.equalTo(self.mViewPhoneBackground);
    }];
    [self.mViewSmsBackground mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mViewPhoneBackground.bottom).offset(1);
        make.leading.trailing.height.equalTo(self.mViewPhoneBackground);
    }];
    [self.mBtnSms mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.trailing.equalTo(self.mViewSmsBackground);
        make.width.equalTo(kIphoneSize_Widith(100));
    }];
    [self.mViewSplit makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.mViewSmsBackground);
        make.width.equalTo(kIphoneSize_Widith(1));
        make.trailing.equalTo(self.mBtnSms.leading);
    }];
    [self.mTextSms mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.mViewSmsBackground);
        make.trailing.equalTo(self.mViewSplit.leading);
        make.leading.equalTo(self.mViewPhoneBackground).offset(kIphoneSize_Widith(10));
    }];
    [self.mViewPwdBackground mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mViewSmsBackground.bottom).offset(1);
        make.leading.height.trailing.equalTo(self.mViewPhoneBackground);
    }];
    [self.mViewPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.equalTo(self.mViewPwdBackground);
        make.width.equalTo(kIphoneSize_Widith(50));
    }];
    [self.mTextPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.mViewPwd.trailing).offset(kIphoneSize_Widith(10));
        make.top.bottom.trailing.equalTo(self.mViewPwdBackground);
    }];
    [self.mBtnTreaty mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mViewPwdBackground.bottom).offset(10);
        make.leading.equalTo(self.view).offset(10);
    }];
    [self.m_btnNext mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mBtnTreaty.bottom).offset(45);
        make.centerX.equalTo(self.view);
        make.leading.trailing.equalTo(self.view).offset(UIEdgeInsetsMake(0, 20, 0, 20));
        make.height.equalTo(56);
    }];
}




#pragma mark - getters and setters

- (void)setFlag:(BOOL)flag
{
    _flag = flag;
    self.mBtnTreaty.hidden = !flag;
}
- (UIView *)mViewPhoneBackground
{
    if (!_mViewPhoneBackground) {
        _mViewPhoneBackground = [[UIView alloc]init];
        _mViewPhoneBackground.backgroundColor = [UIColor whiteColor];
    }
    return _mViewPhoneBackground;
}

- (UIView *)mViewSmsBackground
{
    if (!_mViewSmsBackground) {
        _mViewSmsBackground = [[UIView alloc]init];
        _mViewSmsBackground.backgroundColor = [UIColor whiteColor];
    }
    return _mViewSmsBackground;
}
- (UIView *)mViewSplit
{
    if (!_mViewSplit) {
        _mViewSplit = [[UIView alloc]init];
        _mViewSplit.backgroundColor = kBgColor;
    }
    return _mViewSplit;
}
- (UIView *)mViewPwdBackground
{
    if (!_mViewPwdBackground) {
        _mViewPwdBackground = [[UIView alloc]init];
        _mViewPwdBackground.backgroundColor = [UIColor whiteColor];
    }
    return _mViewPwdBackground;
}
- (TextFieldLeftView *)mViewPhoneLeft
{
    if (!_mViewPhoneLeft) {
        _mViewPhoneLeft = [[TextFieldLeftView alloc]initWithImage:@"phoneicon"];
    }
    return _mViewPhoneLeft;
}
- (UITextField *)mTextPhone
{
    if (!_mTextPhone) {
        _mTextPhone = [[UITextField alloc]init];
        _mTextPhone.leftViewMode = UITextFieldViewModeAlways;
        _mTextPhone.placeholder = @"请输入手机号码";
        _mTextPhone.backgroundColor = [UIColor whiteColor];
        _mTextPhone.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _mTextPhone;
}

- (UITextField *)mTextSms
{
    if (!_mTextSms) {
        _mTextSms = [[UITextField alloc]init];
        _mTextSms.placeholder= @"请输入验证码";
        _mTextSms.keyboardType = UIKeyboardTypeNumberPad;
        _mTextSms.backgroundColor = [UIColor whiteColor];
    }
    return _mTextSms;
}

- (UIButton *)mBtnSms
{
    if (!_mBtnSms) {
        _mBtnSms = [[UIButton alloc]init];
        _mBtnSms.backgroundColor = [UIColor whiteColor];
        _mBtnSms.titleLabel.font = [UIFont systemFontOfSize:13];
        [_mBtnSms setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_mBtnSms setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_mBtnSms addTarget:self action:@selector(mBtnSmsClick) forControlEvents:UIControlEventTouchUpInside];
        _mBtnSms.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    }
    return _mBtnSms;
}

- (TextFieldLeftView *)mViewPwd
{
    if (!_mViewPwd) {
        _mViewPwd = [[TextFieldLeftView alloc]initWithImage:@"passwordicon"];
    }
    return _mViewPwd;
}

- (UITextField *)mTextPwd
{
    if (!_mTextPwd) {
        _mTextPwd = [[UITextField alloc]init];
        _mTextPwd.placeholder = @"请设置登录密码至少6位";
        _mTextPwd.backgroundColor = [UIColor whiteColor];
        _mTextPwd.leftViewMode = UITextFieldViewModeAlways;
        _mTextPwd.secureTextEntry = YES;
    }
    return _mTextPwd;
}

- (UIButton *)mBtnTreaty
{
    if (!_mBtnTreaty) {
        _mBtnTreaty = [[UIButton alloc]init];
        [_mBtnTreaty setTitle:@"我已阅读并同意相关条约" forState:UIControlStateNormal];
        [_mBtnTreaty setImage:[UIImage imageNamed:@"property_icon_selected"] forState:UIControlStateSelected];
        [_mBtnTreaty setImage:[UIImage imageNamed:@"property_icon_normal"] forState:UIControlStateNormal];
        _mBtnTreaty.selected = NO;
        [_mBtnTreaty setTitleColor:RGBColor(51, 51, 51) forState:UIControlStateNormal];
        _mBtnTreaty.titleLabel.font = [UIFont systemFontOfSize:13];
        _mBtnTreaty.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, -10);
        [_mBtnTreaty addTarget:self action:@selector(mBtnTreatyClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _mBtnTreaty;
}
- (UIButton *)m_btnNext
{
    if (!_m_btnNext) {
        _m_btnNext = [[UIButton alloc]init];
        [_m_btnNext setTitle:@"确定" forState:UIControlStateNormal];
        _m_btnNext.backgroundColor = RGBColor(0, 169, 253);
        _m_btnNext.layer.cornerRadius = 5;
        _m_btnNext.layer.masksToBounds = YES;
        [_m_btnNext addTarget:self action:@selector(sure) forControlEvents:UIControlEventTouchUpInside];
    }
    return _m_btnNext;
}
@end
